package com.ling.sdk.utils

import android.os.Bundle
import android.os.Parcelable
import com.alibaba.android.arouter.facade.Postcard
import com.dongby.sdk.utils.DisplayUtils
import java.io.Serializable

/**
 * Created by MinF on 2018/8/1 0001.
 */
fun Postcard.withData(key: String, data: Any?): Postcard {
    when (data) {
        null -> return this
        is Byte -> withByte(key, data)
        is ByteArray -> withByteArray(key, data)
        is Short -> withShort(key, data)
        is ShortArray -> withShortArray(key, data)
        is Int -> withInt(key, data)
        is IntArray -> withIntegerArrayList(key, ArrayList(data.toList()))
        is Long -> withLong(key, data)
        is Float -> withFloat(key, data)
        is FloatArray -> withFloatArray(key, data)
        is Double -> withDouble(key, data)
        is Char -> withChar(key, data)
        is CharArray -> withCharArray(key, data)
        is String -> withString(key, data)
        is Boolean -> withBoolean(key, data)
        is Bundle -> withBundle(key, data)
        is CharSequence -> withCharSequence(key, data)
        is Parcelable -> withParcelable(key, data)
        is Serializable -> withSerializable(key, data)
        else -> withObject(key, data)
    }
    return this

}


fun Int.dip() = DisplayUtils.dip2px(this.toFloat())
fun Int.dipF() = DisplayUtils.dip2px(this.toFloat()).toFloat()

infix fun WeakHandler.delayed(time: Int) = DelayedHandler(time, this)
infix fun DelayedHandler.post(block: () -> Unit) = handler.postDelayed({ block.invoke() }, time.toLong())

class DelayedHandler(val time: Int, val handler: WeakHandler)


